merged master
[lhc/web/wiklou.git] / includes / Revision.php
index 53b792e..a9cc72e 100644 (file)
@@ -404,7 +404,9 @@ class Revision {
                        'page_namespace',
                        'page_title',
                        'page_id',
-                       'page_latest'
+                       'page_latest',
+                       'page_is_redirect',
+                       'page_len',
                );
        }
 
@@ -416,6 +418,29 @@ class Revision {
                return array( 'user_name' );
        }
 
+       /**
+        * Do a batched query to get the parent revision lengths
+        * @param $db DatabaseBase
+        * @param $revIds Array
+        * @return array
+        */
+       public static function getParentLengths( $db, array $revIds ) {
+               $revLens = array();
+               if ( !$revIds ) {
+                       return $revLens; // empty
+               }
+               wfProfileIn( __METHOD__ );
+               $res = $db->select( 'revision',
+                       array( 'rev_id', 'rev_len' ),
+                       array( 'rev_id' => $revIds ),
+                       __METHOD__ );
+               foreach ( $res as $row ) {
+                       $revLens[$row->rev_id] = $row->rev_len;
+               }
+               wfProfileOut( __METHOD__ );
+               return $revLens;
+       }
+
        /**
         * Constructor
         *
@@ -495,7 +520,7 @@ class Revision {
 
                        # if we have a content object, use it to set the model and type
                        if ( !empty( $row['content'] ) ) {
-                               if ( !empty( $row['text_id'] ) ) { #FIXME: when is that set? test with external store setup! check out insertOn()
+                               if ( !empty( $row['text_id'] ) ) { //@todo: when is that set? test with external store setup! check out insertOn() [dk]
                                        throw new MWException( "Text already stored in external store (id {$row['text_id']}), can't serialize content object" );
                                }
 
@@ -540,16 +565,15 @@ class Revision {
                        }
 
                        $this->mTitle     = null; # Load on demand if needed
-                       $this->mCurrent   = false; #XXX: really? we are about to create a revision. it will usually then be the current one.
+                       $this->mCurrent   = false; # XXX: really? we are about to create a revision. it will usually then be the current one.
 
                        # If we still have no length, see it we have the text to figure it out
                        if ( !$this->mSize ) {
                                if ( !is_null( $this->mContent ) ) {
                                        $this->mSize = $this->mContent->getSize();
                                } else {
-                                       #XXX: my be inconsistent with the notion of "size" use for the present content model
-                                       #NOTE: should never happen if we have either text or content object!
-                                       $this->mSize = is_null( $this->mText ) ? null : strlen( $this->mText );
+                                       #NOTE: this should never happen if we have either text or content object!
+                                       $this->mSize = null;
                                }
                        }
 
@@ -564,15 +588,12 @@ class Revision {
                        throw new MWException( 'Revision constructor passed invalid row format.' );
                }
                $this->mUnpatrolled = null;
-
-               // @TODO: add support for ar_content_format, ar_content_model, rev_content_format, rev_content_model to API
-               // @TODO: get rid of $mText
        }
 
        /**
         * Get revision ID
         *
-        * @return Integer
+        * @return Integer|null
         */
        public function getId() {
                return $this->mId;
@@ -591,7 +612,7 @@ class Revision {
        /**
         * Get text row ID
         *
-        * @return Integer
+        * @return Integer|null
         */
        public function getTextId() {
                return $this->mTextId;
@@ -609,7 +630,7 @@ class Revision {
        /**
         * Returns the length of the text in this revision, or null if unknown.
         *
-        * @return Integer
+        * @return Integer|null
         */
        public function getSize() {
                return $this->mSize;
@@ -618,31 +639,37 @@ class Revision {
        /**
         * Returns the base36 sha1 of the text in this revision, or null if unknown.
         *
-        * @return String
+        * @return String|null
         */
        public function getSha1() {
                return $this->mSha1;
        }
 
        /**
-        * Returns the title of the page associated with this entry.
+        * Returns the title of the page associated with this entry or null.
+        *
+        * Will do a query, when title is not set and id is given.
         *
-        * @return Title
+        * @return Title|null
         */
        public function getTitle() {
                if( isset( $this->mTitle ) ) {
                        return $this->mTitle;
                }
-               $dbr = wfGetDB( DB_SLAVE );
-               $row = $dbr->selectRow(
-                       array( 'page', 'revision' ),
-                       self::selectPageFields(),
-                       array( 'page_id=rev_page',
-                                  'rev_id' => $this->mId ),
-                       __METHOD__ );
-               if ( $row ) {
-                       $this->mTitle = Title::newFromRow( $row );
+               if( !is_null( $this->mId ) ) { //rev_id is defined as NOT NULL, but this revision may not yet have been inserted.
+                       $dbr = wfGetDB( DB_SLAVE );
+                       $row = $dbr->selectRow(
+                               array( 'page', 'revision' ),
+                               self::selectPageFields(),
+                               array( 'page_id=rev_page',
+                                          'rev_id' => $this->mId ),
+                               __METHOD__ );
+                       if ( $row ) {
+                               $this->mTitle = Title::newFromRow( $row );
+                       }
                }
+
+               //@todo: as a last resort, perhaps load from page table, if $this->mPage is given?!
                return $this->mTitle;
        }
 
@@ -658,7 +685,7 @@ class Revision {
        /**
         * Get the page ID
         *
-        * @return Integer
+        * @return Integer|null
         */
        public function getPage() {
                return $this->mPage;
@@ -671,7 +698,7 @@ class Revision {
         *
         * @param $audience Integer: one of:
         *      Revision::FOR_PUBLIC       to be displayed to all users
-        *      Revision::FOR_THIS_USER    to be displayed to $wgUser
+        *      Revision::FOR_THIS_USER    to be displayed to the given user
         *      Revision::RAW              get the ID regardless of permissions
         * @param $user User object to check for, only if FOR_THIS_USER is passed
         *              to the $audience parameter
@@ -703,7 +730,7 @@ class Revision {
         *
         * @param $audience Integer: one of:
         *      Revision::FOR_PUBLIC       to be displayed to all users
-        *      Revision::FOR_THIS_USER    to be displayed to $wgUser
+        *      Revision::FOR_THIS_USER    to be displayed to the given user
         *      Revision::RAW              get the text regardless of permissions
         * @param $user User object to check for, only if FOR_THIS_USER is passed
         *              to the $audience parameter
@@ -743,7 +770,7 @@ class Revision {
         *
         * @param $audience Integer: one of:
         *      Revision::FOR_PUBLIC       to be displayed to all users
-        *      Revision::FOR_THIS_USER    to be displayed to $wgUser
+        *      Revision::FOR_THIS_USER    to be displayed to the given user
         *      Revision::RAW              get the text regardless of permissions
         * @param $user User object to check for, only if FOR_THIS_USER is passed
         *              to the $audience parameter
@@ -821,17 +848,18 @@ class Revision {
         *
         * @param $audience Integer: one of:
         *      Revision::FOR_PUBLIC       to be displayed to all users
-        *      Revision::FOR_THIS_USER    to be displayed to $wgUser
+        *      Revision::FOR_THIS_USER    to be displayed to the given user
         *      Revision::RAW              get the text regardless of permissions
         * @param $user User object to check for, only if FOR_THIS_USER is passed
         *              to the $audience parameter
         * @return String
         * @deprecated in 1.WD, use getContent() instead
+        * @todo: replace usage in core
         */
-       public function getText( $audience = self::FOR_PUBLIC, User $user = null ) { #FIXME: deprecated, replace usage! #FIXME: used a LOT!
+       public function getText( $audience = self::FOR_PUBLIC, User $user = null ) {
                wfDeprecated( __METHOD__, '1.WD' );
 
-               $content = $this->getContent();
+               $content = $this->getContent( $audience, $user );
                return ContentHandler::getContentText( $content ); # returns the raw content text, if applicable
        }
 
@@ -875,11 +903,26 @@ class Revision {
         * Fetch revision text without regard for view restrictions
         *
         * @return String
+        *
+        * @deprecated since 1.WD. Instead, use Revision::getContent( Revision::RAW ) or Revision::getSerializedData() as appropriate.
         */
-       public function getRawText() { #FIXME: deprecated, replace usage!
+       public function getRawText() {
+               wfDeprecated( __METHOD__, "1.WD" );
+
                return $this->getText( self::RAW );
        }
 
+       /**
+        * Fetch original serialized data without regard for view restrictions
+        *
+        * @return String
+        *
+        * @since 1.WD
+        */
+       public function getSerializedData() {
+               return $this->mText;
+       }
+
        protected function getContentInternal() {
                if( is_null( $this->mContent ) ) {
                        // Revision is immutable. Load on demand:
@@ -948,7 +991,14 @@ class Revision {
                        $model = $this->getContentModel();
                        $this->mContentHandler = ContentHandler::getForModelID( $model );
 
-                       assert( $this->mContentHandler->isSupportedFormat( $this->getContentFormat() ) );
+                       $format = $this->getContentFormat();
+
+                       if ( !$this->mContentHandler->isSupportedFormat( $format ) ) {
+                               $formatName = ContentHandler::getContentFormatMimeType( $format );
+                               $modelName = ContentHandler::getContentModelName( $model );
+
+                               throw new MWException( "Oops, the content format #$format ($formatName) is not supported for this content model, #$model ($modelName)" );
+                       }
                }
 
                return $this->mContentHandler;
@@ -1200,6 +1250,8 @@ class Revision {
                        $row[ 'rev_content_format' ] = $this->getContentFormat();
                }
 
+               $this->checkContentModel();
+
                $dbw->insert( 'revision', $row, __METHOD__ );
 
                $this->mId = !is_null( $rev_id ) ? $rev_id : $dbw->insertId();
@@ -1210,6 +1262,57 @@ class Revision {
                return $this->mId;
        }
 
+       protected function checkContentModel() {
+               global $wgContentHandlerUseDB;
+
+               $title = $this->getTitle(); //note: returns null for revisions that have not yet been inserted.
+
+               $model = $this->getContentModel();
+               $format = $this->getContentFormat();
+               $handler = $this->getContentHandler();
+
+               if ( !$handler->isSupportedFormat( $format ) ) {
+                       $t = $title->getPrefixedDBkey();
+                       $modelName = ContentHandler::getContentModelName( $model );
+                       $formatName = ContentHandler::getContentFormatMimeType( $format );
+
+                       throw new MWException( "Can't use format #$format ($formatName) with content model #$model ($modelName) on $t" );
+               }
+
+               if ( !$wgContentHandlerUseDB && $title ) {
+                       // if $wgContentHandlerUseDB is not set, all revisions must use the default content model and format.
+
+                       $defaultModel = ContentHandler::getDefaultModelFor( $title );
+                       $defaultHandler = ContentHandler::getForModelID( $defaultModel );
+                       $defaultFormat = $defaultHandler->getDefaultFormat();
+
+                       if ( $this->getContentModel() != $defaultModel ) {
+                               $defaultModelName = ContentHandler::getContentModelName( $defaultModel );
+                               $modelName = ContentHandler::getContentModelName( $model );
+                               $t = $title->getPrefixedDBkey();
+
+                               throw new MWException( "Can't save non-default content model with \$wgContentHandlerUseDB disabled: model is #$model ($modelName), default for $t is #$defaultModel ($defaultModelName)" );
+                       }
+
+                       if ( $this->getContentFormat() != $defaultFormat ) {
+                               $defaultFormatName = ContentHandler::getContentFormatMimeType( $defaultFormat );
+                               $formatName = ContentHandler::getContentFormatMimeType( $format );
+                               $t = $title->getPrefixedDBkey();
+
+                               throw new MWException( "Can't use non-default content format with \$wgContentHandlerUseDB disabled: format is #$format ($formatName), default for $t is #$defaultFormat ($defaultFormatName)" );
+                       }
+               }
+
+               $content = $this->getContent( Revision::RAW );
+
+               if ( !$content->isValid() ) {
+                       $t = $title->getPrefixedDBkey();
+                       $modelName = ContentHandler::getContentModelName( $model );
+
+                       throw new MWException( "Content of $t is not valid! Content model is #$model ($modelName)" );
+               }
+       }
+
        /**
         * Get the base 36 SHA-1 value for a string of text
         * @param $text String
@@ -1298,7 +1401,8 @@ class Revision {
 
                wfProfileIn( __METHOD__ );
 
-               $fields = array( 'page_latest', 'rev_text_id', 'rev_len', 'rev_sha1' );
+               $fields = array( 'page_latest', 'page_namespace', 'page_title',
+                                               'rev_text_id', 'rev_len', 'rev_sha1' );
 
                if ( $wgContentHandlerUseDB ) {
                        $fields[] = 'rev_content_model';
@@ -1322,7 +1426,7 @@ class Revision {
                                'text_id'    => $current->rev_text_id,
                                'parent_id'  => $current->page_latest,
                                'len'        => $current->rev_len,
-                               'sha1'       => $current->rev_sha1,
+                               'sha1'       => $current->rev_sha1
                        );
 
                        if ( $wgContentHandlerUseDB ) {
@@ -1331,6 +1435,7 @@ class Revision {
                        }
 
                        $revision = new Revision( $row );
+                       $revision->setTitle( Title::makeTitle( $current->page_namespace, $current->page_title ) );
                } else {
                        $revision = null;
                }